import pytest
import allure
from playwright.sync_api import Page, expect

@allure.feature("密码管理")
@allure.story("忘记密码功能")
class TestForgotPassword:
    
    @allure.title("测试忘记密码页面基本元素")
    @allure.severity(allure.severity_level.NORMAL)
    def test_forgot_password_page_elements(self, page: Page):
        """测试忘记密码页面的基本元素是否存在"""
        with allure.step("导航到忘记密码页面"):
            page.goto("http://localhost:8080/forgotpassword")
        
        with allure.step("检查页面标题"):
            expect(page.get_by_text("重置密码")).to_be_visible()
        
        with allure.step("检查输入字段"):
            expect(page.locator('input[placeholder="请输入用户名"]')).to_be_visible()
            expect(page.locator('input[placeholder="请输入邮箱"]')).to_be_visible()
            expect(page.locator('input[placeholder="请输入验证码"]')).to_be_visible()
            expect(page.locator('input[placeholder="请输入新密码"]')).to_be_visible()
            expect(page.locator('input[placeholder="请确认新密码"]')).to_be_visible()
        
        with allure.step("检查按钮"):
            expect(page.get_by_role("button", name="获取验证码")).to_be_visible()
            expect(page.get_by_role("button", name="重置密码")).to_be_visible()
        
        with allure.step("检查返回登录链接"):
            expect(page.get_by_text("返回登录")).to_be_visible()

    @allure.title("测试请求验证码功能")
    @allure.severity(allure.severity_level.NORMAL)
    def test_request_verification_code(self, page: Page):
        """测试请求验证码功能"""
        with allure.step("导航到忘记密码页面"):
            page.goto("http://localhost:8080/forgotpassword")
        
        with allure.step("填写用户名和邮箱"):
            page.locator('input[placeholder="请输入用户名"]').fill("common")
            page.locator('input[placeholder="请输入邮箱"]').fill("2582343251@qq.com")
        
        with allure.step("点击获取验证码按钮"):
            page.get_by_role("button", name="获取验证码").click()
        
        with allure.step("验证成功提示"):
            success_message = page.locator('.el-message--success')
            expect(success_message).to_be_visible()
            expect(success_message).to_contain_text("验证码已发送")
        
        with allure.step("验证按钮是否被禁用（倒计时）"):
            expect(page.get_by_role("button", name="获取验证码")).to_be_disabled()

    @allure.title("测试重置密码成功场景")
    @allure.severity(allure.severity_level.CRITICAL)
    def test_reset_password_success(self, page: Page):
        """测试重置密码成功场景"""
        with allure.step("导航到忘记密码页面"):
            page.goto("http://localhost:8080/forgotpassword")
        
        with allure.step("填写所有字段"):
            page.locator('input[placeholder="请输入用户名"]').fill("common")
            page.locator('input[placeholder="请输入邮箱"]').fill("test@example.com")
            page.locator('input[placeholder="请输入验证码"]').fill("123456")
            page.locator('input[placeholder="请输入新密码"]').fill("NewPassword123!")
            page.locator('input[placeholder="请确认新密码"]').fill("NewPassword123!")
        
        with allure.step("点击重置密码按钮"):
            page.get_by_role("button", name="重置密码").click()
        
        with allure.step("验证成功提示"):
            success_message = page.locator('.el-message--success')
            expect(success_message).to_be_visible()
            expect(success_message).to_contain_text("密码重置成功")
        
        with allure.step("验证是否跳转到登录页面"):
            page.wait_for_url("**/login")

    @allure.title("测试新密码不匹配场景")
    @allure.severity(allure.severity_level.NORMAL)
    def test_password_mismatch(self, page: Page):
        """测试新密码不匹配场景"""
        with allure.step("导航到忘记密码页面"):
            page.goto("http://localhost:8080/forgotpassword")
        
        with allure.step("填写不匹配的新密码"):
            page.locator('input[placeholder="请输入用户名"]').fill("testuser")
            page.locator('input[placeholder="请输入邮箱"]').fill("test@example.com")
            page.locator('input[placeholder="请输入验证码"]').fill("123456")
            page.locator('input[placeholder="请输入新密码"]').fill("NewPassword123!")
            page.locator('input[placeholder="请确认新密码"]').fill("DifferentPassword!")
        
        with allure.step("点击重置密码按钮"):
            page.get_by_role("button", name="重置密码").click()
        
        with allure.step("验证错误提示"):
            error_message = page.locator('.el-message--error')
            expect(error_message).to_be_visible()
            expect(error_message).to_contain_text("两次输入的密码不一致")

    @allure.title("测试无效验证码场景")
    @allure.severity(allure.severity_level.NORMAL)
    def test_invalid_verification_code(self, page: Page):
        """测试无效验证码场景"""
        with allure.step("导航到忘记密码页面"):
            page.goto("http://localhost:8080/forgotpassword")
        
        with allure.step("填写无效的验证码"):
            page.locator('input[placeholder="请输入用户名"]').fill("testuser")
            page.locator('input[placeholder="请输入邮箱"]').fill("test@example.com")
            page.locator('input[placeholder="请输入验证码"]').fill("000000")
            page.locator('input[placeholder="请输入新密码"]').fill("NewPassword123!")
            page.locator('input[placeholder="请确认新密码"]').fill("NewPassword123!")
        
        with allure.step("点击重置密码按钮"):
            page.get_by_role("button", name="重置密码").click()
        
        with allure.step("验证错误提示"):
            error_message = page.locator('.el-message--error')
            expect(error_message).to_be_visible()
            expect(error_message).to_contain_text("验证码无效或已过期")

    @allure.title("测试返回登录页面链接")
    @allure.severity(allure.severity_level.MINOR)
    def test_navigation_to_login(self, page: Page):
        """测试返回登录页面链接"""
        with allure.step("导航到忘记密码页面"):
            page.goto("http://localhost:8080/forgotpassword")
        
        with allure.step("点击返回登录链接"):
            page.get_by_text("返回登录").click()
        
        with allure.step("验证是否返回到登录页面"):
            page.wait_for_url("**/login") 